package com.ruoyi.forum.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.forum.domain.vo.SchoolRank;
import com.ruoyi.forum.mapper.ForumCollegeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.forum.domain.ForumCollege;
import com.ruoyi.forum.mapper.ForumSchoolMapper;
import com.ruoyi.forum.domain.ForumSchool;
import com.ruoyi.forum.service.IForumSchoolService;

/**
 * 学校管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-01
 */
@Service
public class ForumSchoolServiceImpl implements IForumSchoolService 
{
    @Autowired
    private ForumSchoolMapper forumSchoolMapper;

    @Autowired
    private ForumCollegeMapper forumCollegeMapper;

    /**
     * 查询学校管理
     * 
     * @param id 学校管理主键
     * @return 学校管理
     */
    @Override
    public ForumSchool selectForumSchoolById(Long id)
    {
        ForumSchool forumSchool = forumSchoolMapper.selectForumSchoolById(id);
        ForumCollege forumCollege = new ForumCollege();
        forumCollege.setSchoolId(forumSchool.getId());
        forumCollege.setDelFlag("0");
        forumSchool.setForumCollegeList(forumCollegeMapper.selectForumCollegeList(forumCollege));
        return forumSchool;
    }

    /**
     * 查询学校管理列表
     * 
     * @param forumSchool 学校管理
     * @return 学校管理
     */
    @Override
    public List<ForumSchool> selectForumSchoolList(ForumSchool forumSchool)
    {
        return forumSchoolMapper.selectForumSchoolList(forumSchool);
    }

    /**
     * 查询当天学校热度排行
     *
     * @param date 查询时间
     * @return 集合
     */
    @Override
    public List<SchoolRank> selectForumSchoolRankForDate(Date date) {
        return forumSchoolMapper.selectForumSchoolRankForDate(date);
    }

    /**
     * 新增学校管理
     * 
     * @param forumSchool 学校管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertForumSchool(ForumSchool forumSchool)
    {
        forumSchool.setCreateTime(DateUtils.getNowDate());
        int rows = forumSchoolMapper.insertForumSchool(forumSchool);
        insertForumCollege(forumSchool);
        return rows;
    }

    /**
     * 修改学校管理
     * 
     * @param forumSchool 学校管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateForumSchool(ForumSchool forumSchool)
    {
        forumSchool.setUpdateTime(DateUtils.getNowDate());
        forumSchoolMapper.deleteForumCollegeBySchoolId(forumSchool.getId());
        insertForumCollege(forumSchool);
        return forumSchoolMapper.updateForumSchool(forumSchool);
    }

    /**
     * 批量删除学校管理
     * 
     * @param ids 需要删除的学校管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteForumSchoolByIds(Long[] ids)
    {
        forumSchoolMapper.deleteForumCollegeBySchoolIds(ids);
        return forumSchoolMapper.deleteForumSchoolByIds(ids);
    }

    /**
     * 删除学校管理信息
     * 
     * @param id 学校管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteForumSchoolById(Long id)
    {
        forumSchoolMapper.deleteForumCollegeBySchoolId(id);
        return forumSchoolMapper.deleteForumSchoolById(id);
    }

    /**
     * 新增学院管理信息
     * 
     * @param forumSchool 学校管理对象
     */
    public void insertForumCollege(ForumSchool forumSchool)
    {
        List<ForumCollege> forumCollegeList = forumSchool.getForumCollegeList();
        Long id = forumSchool.getId();
        if (StringUtils.isNotNull(forumCollegeList))
        {
            List<ForumCollege> list = new ArrayList<ForumCollege>();
            for (ForumCollege forumCollege : forumCollegeList)
            {
                forumCollege.setSchoolId(id);
                if(!StringUtils.isNotNull(forumCollege.getDelFlag())){
                    forumCollege.setDelFlag("0");
                }
                list.add(forumCollege);
            }
            if (list.size() > 0)
            {
                forumSchoolMapper.batchForumCollege(list);
            }
        }
    }
}
